<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMorphSkuTables extends Migration
{
    public function up()
    {
        $tableNames = config('hon-sku.table_names');

        Schema::create($tableNames['skus'], function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->morphs('producible');
            $table->decimal('amount'); //数量
            $table->unsignedInteger('stock'); //库存
            $table->timestamps();
        });

        Schema::create($tableNames['options'], function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->unique();
            $table->timestamps();
        });

        Schema::create($tableNames['attrs'], function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->morphs('producible');
            $table->unsignedBigInteger('option_id')->index();
            $table->string('value');
            $table->timestamps();
        });

        Schema::create($tableNames['attr_sku'], function (Blueprint $table) {
            $table->unsignedBigInteger('sku_id')->index();
            $table->unsignedBigInteger('attr_id')->index();
            $table->unique(['sku_id', 'attr_id']);
        });
    }

    public function down()
    {
        $tableNames = config('hon-sku.table_names');

        Schema::dropIfExists($tableNames['skus']);
        Schema::dropIfExists($tableNames['options']);
        Schema::dropIfExists($tableNames['attrs']);
        Schema::dropIfExists($tableNames['attr_sku']);
    }
}
